//
//  SortSleepDetailView.m
//  project
//
//  Created by 黄建华 on 2019/1/8.
//  Copyright © 2019年 黄建华. All rights reserved.
//

#import "SortSleepDetailView.h"

@implementation SortSleepDetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self performSelector:@selector(createChartView) withObject:nil afterDelay:IOSINITWAITTIME];
    }
    return self;
}

- (void)createChartView {
    self.backgroundColor = [UIColor whiteColor];
    if (!_timeLabel) {
        _timeLabel = [UILabel simpleWithRect:SNFRAME(0,0,self.width, 20) withAlignment:NSTextAlignmentCenter withFontSize:14.0 withText:@"您在" withTextColor:[UIColor blackColor]];
        [self addSubview:_timeLabel];
    }
    NSArray *array      = [NSArray arrayWithObjects:SNSLEEPCOLORDEEP, SNSLEEPCOLORLIGHT, SNSLEEPCOLORSOBER, nil];
    for (int i = 0; i <3; i ++) {
        UIView *view = [[UIView alloc] initWithFrame:SNFRAME(HHHWIDTH/2.0 -100 +i*15, _timeLabel.bottom +15, 15, 15)];
        view.backgroundColor = [array objectAtIndex:i];
        [self addSubview:view];
        
        if (i == 2) {
            UILabel *label = [UILabel simpleWithRect:SNFRAME(view.right +10, view.top, 60, 15) withAlignment:NSTextAlignmentLeft withFontSize:12.0 withText:SN_Text(@"今天") withTextColor:[UIColor blackColor]];
            [self addSubview:label];
            
            DrawView *dog = [[DrawView alloc] initWithFrame:SNFRAME(HHHWIDTH/2.0 +20, view.top-4, 4, view.height +8)];
            [self addSubview:dog];
            
            UILabel *label2 = [UILabel simpleWithRect:SNFRAME(dog.right +10, view.top, 100, 15) withAlignment:NSTextAlignmentLeft withFontSize:12.0 withText:SN_Text(@"平均") withTextColor:[UIColor blackColor]];
            [self addSubview:label2];
        }
    }
    
    CGFloat height = (self.height*0.6 -30)/3.0;
    
    _deep = [[SleepChartView alloc] initWithFrame:SNFRAME(30,self.height*0.40,self.width - 60 -100, height)];
    [self addSubview:_deep];
    
    _light = [[SleepChartView alloc] initWithFrame:SNFRAME(30,_deep.bottom +10,_deep.width, height)];
    [self addSubview:_light];
    
    _weak = [[SleepChartView alloc] initWithFrame:SNFRAME(30,_light.bottom +10, _deep.width, height)];
    [self addSubview:_weak];
    
    [self UpdateViewWithType:SelectDateWeek];
}

- (void)UpdateViewWithType:(SelectDateType)type
{
//    SN_PublicClass.selectDate
    NSString *string = [self showDateWithType:type];
    NSArray *time = [SN_PublicClass getBeginDateStringAndEndWith:[NSDate date] withType:type];
    _timeLabel.text = [NSString stringWithFormat:@"%@ %@ %@",SN_Text(@"您在"),string,SN_Text(@"平均时间")];
    
    NSString *deepS  = [NSString stringWithFormat:@"SELECT SUM(deeps) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *deep = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:deepS arguments:nil];
    if (deep.length == 0) {
        deep = @"0";
    }
    NSString *lightS  = [NSString stringWithFormat:@"SELECT SUM(lights) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *light = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:lightS arguments:nil];
    if (light.length == 0) {
        light = @"0";
    }
    NSString *soberS  = [NSString stringWithFormat:@"SELECT SUM(sobers) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *sober = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:soberS arguments:nil];
    if (sober.length == 0) {
        sober = @"0";
    }
    NSString *totalS  = [NSString stringWithFormat:@"SELECT SUM(durations) AS OrderTotal FROM SleepModel WHERE dateString >='%@' and dateString <='%@' and durations >0 and user_id = '%@'", [time firstObject], [time lastObject], SN_USER.user_id];
    NSString *total = [[SleepModel getUsingLKDBHelper]executeScalarWithSQL:totalS arguments:nil];
    if (total.length == 0) {
        total = @"0";
    }
    
    SleepModel *selectSleep = [SleepModel getSleepModelFromDBWith:[NSDate date]];
    NSInteger durations = selectSleep.durations;
    NSInteger deeps = selectSleep.deeps;
    NSInteger lights = selectSleep.lights;
    NSInteger sobers = selectSleep.sobers;

    NSInteger deepsAV = (int)(1.0*deep.intValue / total.intValue *100);
    NSInteger lightAV = (int)(1.0*light.intValue / total.intValue *100);
    NSInteger soberAV = (int)(1.0*sober.intValue / total.intValue *100);
    
    if (_deep) {
     [_deep updateWith:(int)(1.0*deeps/durations *100) aveValue:deepsAV withType:0];
    }
    if (_light) {
         [_light updateWith:(int)(1.0*lights/durations*100) aveValue:lightAV withType:1];
    }
    if (_weak) {
        [_weak updateWith:(int)(1.0*sobers/durations*100) aveValue:soberAV withType:2];
    }
    
//    if (_deep) {
//        [_deep updateWith:100*deeps/durations aveValue:deepsAV withType:0];
//    }
//    if (_light) {
//        [_light updateWith:100*lights/durations aveValue:lightAV withType:1];
//    }
//    if (_weak) {
//        [_weak updateWith:100*sobers/durations aveValue:soberAV withType:2];
//    }
    
//    [_deep updateWith:90 aveValue:100 withType:0];
//    [_light updateWith:90 aveValue:100 withType:1];
//    [_weak updateWith:90 aveValue:100 withType:2];
}

//周 月 年显示日期
- (NSString *)showDateWithType:(SelectDateType)type {
    NSString *dateString = @"";
    
    if (type == 0) {
        if ([[SN_PublicClass.selectDate dateToDayString] isEqualToString:[[NSDate date]dateToDayString]]) {
            dateString = SN_Text(@"今天");
        } else {
            dateString = [SN_PublicClass.selectDate dateToDayString];
        }
    } else {
        NSDate *beginDate = nil;
        NSDate *endDate   = nil;
        NSDate *newDate   = [NSDate date];
        if (type == 1) {
            beginDate = [newDate dateByAddingDays:-newDate.weekday  +1];
            endDate   = [newDate dateByAddingDays:-newDate.weekday  +7];
        } else if (type == 2) {
            beginDate = [newDate dateByAddingDays:-newDate.day +1];
            endDate   = [newDate dateByAddingDays:-newDate.day + newDate.daysInMonth];
        } else if (type == 3) {
            beginDate = [newDate dateByAddingDays:-newDate.dayOfYear +1];
            endDate   = [newDate dateByAddingDays:-newDate.dayOfYear + newDate.daysInYear];
        }
        
        if (type == 1) {
            dateString = [NSString stringWithFormat:@"%@- %@", [beginDate dateMonthAndDay2], [endDate dateMonthAndDay2]];
        } else if (type == 2) {
            dateString = [NSString stringWithFormat:@"%@- %@", [beginDate dateMonthAndDay2], [endDate dateMonthAndDay2]];
        } else if (type == 3) {
            dateString = [NSString stringWithFormat:@"%ld/01 - %ld/12", (long)[NSDate date].year, (long)[NSDate date].year];
        } else if (type == 4) {
            if ([NSDate date].month > 4) {
                dateString = [NSString stringWithFormat:@"%ld/%ld - %ld/%ld", (long)[NSDate date].year,[NSDate date].month - 3, (long)[NSDate date].year,[NSDate date].month];
            }
            
        }
    }
    return dateString;
}

@end
